/*
* name;
*/
var ByteArray = (function (_super) {
    function ByteArray(data) {
        ByteArray.super(this);
        this._position=0;
        this._length=0;
        this._byteView=new Uint8Array(data);
        this._allocated=8;
        this._dataView=new Uint8Array(data);
        this.data=data;
        this._littleEndian=false;
    }
    Laya.class(ByteArray,"ByteArray",_super);

    var _proto=ByteArray.prototype;

    _proto.inite=function()
    {

    }



     _proto.ensureWrite=function(lengthToEnsure)
    {
        if(this.length<lengthToEnsure)
        {
            this.length=lengthToEnsure;
        }
    }

     _proto.readBoolean=function()
    {
        return (this.readByte()!=0);
    }

    _proto.readByte=function()
    {
        return this.data.getInt8(this._position++);
    }

    _proto.readBytes=function(arraybuffer,offset=0,length=0)
    {
        if(offset<0||length<0)
        {
            console.error("writeArraybuffer Erro,out of bounds");
        }
        if(length==0) //length=0的时候重新赋值
        {
           length= arraybuffer.byteLength-offset;
        }
        this.ensureWrite(offset+length);

        this._byteView.set(this._byteView.subarray(this._position,this._position+length),offset);

        this._position+=length;

        

    }

    _proto.readDouble=function()
    {
        var double=this.data.getFloat64(this._position,this._littleEndian);
        this._position+=8;
        return double;
    }

    _proto.readFloat=function()
    {
        var float=this.data.getFloat32(this._position,this._littleEndian);
        this._position+=4;
        return float;
    }

    _proto.readFullBytes=function(byte,pos,len)
    {
        this.ensureWrite(len);
        var i;
        for(i=0;i<pos+len;i++)
        {
            this.data.setInt8(this._position++,byte.get(i));
        }
    }


    _proto.readInt=function()
    {
        var tInt=this.data.getInt3(this._position,this._littleEndian);
        this._position+=4;
        tInt=this.Int(tInt);
        return tInt;
    }

    _proto.readShort=function()
    {
        var short=this.data.getInt16(this._position,this._littleEndian);
        this._position+=2;
        short=this.Int(short);
        return short;
    }

    _proto.readUnsignedByte=function()
    {
        return this.data.getUint8(this._position++);
    }

    _proto.readUnsignedInt=function()
    {
        var uInt=this.data.getUint32(this._position,this._littleEndian);
        this._position+=4;
        uInt=this.Int(uInt);
        uInt=this.right(uInt);
        return uInt;  //解决读取整数的时候读到负整数
    }

    _proto.readUnsignedShrot=function()
    {
        var ushort=this.data.getUint16(this._position,this._littleEndian);
        this._position+=2;
        ushort=this.Int(ushort);
        return ushort;
    }

    _proto.readUTF=function()
    {
        return this.readUTFBytes(this.readUnsignedShrot());
    }

    _proto.readUnicode=function(length)
    {
        var value="";
        var max=this._position+length;
        var c1,c2;
        while(this._position<max)
        {
            c2=this._byteView[this._position++];
            c1=this._byteView[this._position++];
            value+=String.fromCharCode(c1<<8|c2);
        }
        return value;
    }

    _proto.readMultiByte=function(length,charset)
    {
        if(charset=="UNICODE"||charset=="unicode")
        {
            return this.readUnicode(length);
        }
        return this.readUTFBytes(length);
    }

    _proto.readUTFBytes=function(length)
    {
        var value="";
        var max=this._position+length;
        var c,c2,v3;
        while(this._position<max)
        {
            c=this.data.getUint8(this._position++)
            if(c<0x80)
            {
                if(c!=0)
                {
                    value +=String.fromCharCode(c);
                }
            }
            else if(c<0xE0)
            {
                value+=String.fromCharCode(((c&0x3F)<<6)|(this.data.getUint8(this._position++)&0x7F));
            }
            else if(c<0xF0)
            {
                c2=this.data.getUint8(this._position++);
                value +=String.fromCharCode(((c & 0x1F) << 12) | ((c2 & 0x7F) << 6) | (_data_.getUint8 (this._position++) & 0x7F));
            }
            else{
                c2=this.data.getUint8(this._position++);
                c3=this.data.getUint8(this._position++);
                value += String.fromCharCode (((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 << 6) & 0x7F) | (this.data.getUint8 (this._position++) & 0x7F));
            }

        }
        return value;
    }

    _proto.toString=function()
    {
        var cachePosition=this._position;
        this._position=0;
        var value=this.readUTFBytes(this.length);
        this._position=cachePosition;
        return value;
    }

    _proto.writeBoolean=function(value)  //输入的是boolean类型 
    {
        writeByte(value?1:0);
    }

    _proto.writeByte=function(value) //int值
    {
        ensureWrite(this._position+1);
        this.data.setInt8(this._position,value);
        this._position+=1;
    }

    _proto.writeBytes=function(bytes,offset,lenth)
    {
        if(offset<0||length<0)
        {
            console.error("writeArraybuffer Erro,out of bounds");
        }
        if(length==0) //length=0的时候重新赋值
        {
           length= arraybuffer.byteLength-offset;
        }
         this.ensureWrite(this._position+length);
         this._byteView.set(this._byteView.subarray(offset,offset+length),this._position)
         this._position+=length;

    }



    

    _proto.writeArraybuffer=function(arraybuffer,offset=0,length=0)
    {
        if(offset<0||length<0)
        {
            console.error("writeArraybuffer Erro,out of bounds");
        }
        if(length==0) //length=0的时候重新赋值
        {
           length= arraybuffer.byteLength-offset;
        }
      

        this.ensureWrite(this._position+length);
        var uinte8array=new Uint8Array(arraybuffer); //Uinte8array(arraybuffer);
      //  this._dataView=new DataView(arraybuffer);
       
        this._dataView.set(uinte8array.subarray(offset,offset+length),this._position);
       // this._dataView=new DataView(arraybuffer,this._position,uinte8array.subarray(offset,offset+length).length);
         //this._byteView=new Uint8Array(arraybuffer);
        // this._dataView.set(uinte8array.subarray(offset,offset+length),this._position);
        this._position+=length;
    }

    _proto.writeDouble=function(x)
    {
        this.ensureWrite(this._position+8);
        this.data.setFloat64(this._position,x,this._littleEndian);
        this._position+=8;
    }

    _proto.writeFloat=function(x)
    {
        this.ensureWrite(this._position+4);
        this.data.setFloat64(this._position,x,this._littleEndian);
        this._position+=4;
    }

    _proto.writeInt=function(value)
    {
        this.ensureWrite(this._position+4);
        this.data.setInt32(this._position,value,this._littleEndian);
        this._position+=4;
    }

    _proto.writeShort=function(value)
    {
        this.ensureWrite(this._position+2);
        this.data.setInt16(this._position,value,this._littleEndian);
        this._position+=2;
    }

    _proto.writeUnsignedInt=function(value)
    {
        this.ensureWrite(this._position+4);
        this.data.setUnit32(this._position,value,this._littleEndian);
        this._position+=4;
    }

    _proto.writeUnsignedShort=function(value)
    {
        this.ensureWrite(this._position+2);
        this.data.setUnit16(this._position,value,this._littleEndian);
        this._position+=2;
    }




    _proto.uncompress=function(algorithm)
    {
        var inflate=new Zlib.Inflate(this._byteView);
        var a=inflate.decompress();

    }

    _proto.compress=function()
    {
        var deflate=new Zlib.Deflate(this._byteView);
        var b=deflate.compress();
    }


    _proto.Int=function(i)
    {
        var n=i%1;
        i=i-n;
        return i;
    }

    _proto.right=function(i)
    {
        if(i<0)
        {
            i=-i
        }
        
        return i;
    }
   




    return ByteArray;
})(Laya.Script);